package cn.ycc1.spark.config;

import cn.ycc1.spark.BigModelSpark;
import cn.ycc1.spark.util.CompletableTask;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author ycc
 * @date 2024/5/4
 */
@Component
@RequiredArgsConstructor
public class StartupConfiguration {
    private final SparkConfig sparkConfig;
    private final BigModelSpark bigModelSpark;
    private final Executor myCompletableTaskExecutor;
    private final ReentrantLock lock = new ReentrantLock();

    @PostConstruct
    public void init() throws Exception {
        // 在这里调用您的服务方法
        System.out.println("启动成功 测试应用启动成功 执行方法调用");
        // 新开一个线程执行这个方法
        // bigModelSpark.test(sparkConfig.getHostUrl(), sparkConfig.getAppid(), sparkConfig.getApiSecret(), sparkConfig.getApiKey());
        CompletableTask.allOf(
                myCompletableTaskExecutor,
                () -> {
                    lock.lock();
                    try {
                        bigModelSpark.test(sparkConfig.getHostUrl(), sparkConfig.getAppid(), sparkConfig.getApiSecret(), sparkConfig.getApiKey());
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        lock.unlock();
                    }
                }
        );
    }
}
